import path from 'path'
import fs from 'fs'
import { MockMethod/* , RespThisType */ } from 'vite-plugin-mock'

export default [
  {
    url: '/file/upload',
    method: 'post',
    response: () => {
      return {
        code: 1000,
        data: /\w{10}/,
        message: '处理成功'
      }
    }
  },
  {
    url: '/file/remove',
    method: 'post',
    response: () => {
      return {
        code: 1000,
        data: '',
        message: '处理成功'
      }
    }
  },
  {
    url: '/file/download/image',
    method: 'get',
    rawResponse: (req, res) => {
      // console.log('req', req)
      // console.log('res', res)

      // res.setHeader('Access-Control-Allow-Origin', '*')

      /* const options = {
        root: path.join(__dirname, 'images'),
        dotfiles: 'allow',
        headers: {
          'content-disposition': 'inline;filename=1.jpg',
          'Content-Type': 'image/jpg;charset=UTF-8',
          'x-timestamp': Date.now(),
          'x-sent': true
        }
      } */

      // const fileName = req.params.key
      // res.attachment('1.jpg')
      // res.setHeader('Content-Type','image/jpeg;charset=UTF-8')
      // res.sendFile('1.jpg', options)
      // console.log(res)
      // res.redirect('http://192.168.137.1:8081/1.jpg')

      // 图片文件路径
      const imagePath = path.join(__dirname, 'images/1.jpg')
      // 读取图片文件为 Buffer
      const imageBuffer = fs.readFileSync(imagePath)
      // 设置响应的 Content-Type
      res.setHeader('content-disposition', 'inline;filename=1.jpg')
      res.setHeader('Content-Type', 'image/jpeg')

      // 设置响应的 Content-Length
      res.setHeader('Content-Length', imageBuffer.length)

      // 发送二进制流作为响应体
      res.end(imageBuffer)
    }
  },
  {
    url: '/file/download/pdf',
    method: 'get',
    rawResponse: (req, res) => {
      // 图片文件路径
      const pdfPath = path.join(__dirname, 'pdf/1.pdf')
      // 读取图片文件为 Buffer
      const pdfBuffer = fs.readFileSync(pdfPath)
      // 设置响应的 Content-Type
      res.setHeader('Access-Control-Allow-Origin', '*')
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
      res.setHeader('Access-Control-Allow-Headers', 'Content-Disposition')
      res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition')

      res.setHeader('Content-Disposition', 'inline;filename=1.pdf')
      res.setHeader('Content-Type', 'application/pdf')

      // 设置响应的 Content-Length
      res.setHeader('Content-Length', pdfBuffer.length)

      // 发送二进制流作为响应体
      res.end(pdfBuffer)
    }
  },
  {
    url: '/file/download/pdf/slice',
    method: 'get',
    rawResponse: (req, res) => {
      // 图片文件路径
      const pdfPath = path.join(__dirname, 'pdf/1.pdf')
      // 读取图片文件为 Buffer
      const pdfBuffer = fs.readFileSync(pdfPath)
      // 设置响应的 Content-Type
      res.setHeader('content-disposition', 'attachment;filename=1.pdf')
      res.setHeader('Content-Type', 'application/octect-stream;charset=UTF-8')
      res.setHeader('Accept-Ranges', 'bytes')

      const { range: rangeHeader } = req.headers
      const fileSize = pdfBuffer.length
      if (!rangeHeader) {
        res.setHeader('Content-Length', fileSize)
        // res.setHeader("Buffer-Size", pdfBuffer.length);
        // res.setHeader("Access-Control-Expose-Headers", "Buffer-Size");
        // 发送二进制流作为响应体
        res.end(pdfBuffer)
      } else {
        //分片
        let start = 0
        let end = fileSize - 1
        const range = rangeHeader.split('=')[1].split('-')
        if (range.length == 1) {
          start = +range[0]
          end = +fileSize - 1
        } else {
          start = +range[0]
          end = +range[1]
        }

        const contentLength = end - start
        res.setHeader('Content-Length', contentLength)
        res.setHeader('Content-Range', 'bytes' + start + '-' + end + '/' + fileSize)
        // 发送二进制流作为响应体
        res.end(pdfBuffer.slice(start, end))
      }


    }
  },
  {
    url: '/file/ofd',
    method: 'get',
    response: (/* { req, res }: RespThisType */) => {
      /* const {
        query: { key }
      } = req

      const options = {
        root: path.join(__dirname, 'file'),
        dotfiles: 'allow',
        headers: {
          'content-disposition': 'inline',
          'Content-Type': 'charset=UTF-8',
          'x-timestamp': Date.now(),
          'x-sent': true
        }
      }

      res.sendFile(key + '.ofd', options) */
    }
  }
] as MockMethod[]
